package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dto.OrderSearchDto;
import com.example.dto.ReturnApplyDto;
import com.example.entity.OrderReturnApply;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
* @author circle48
* @description 针对表【order_return_apply(订单退货申请)】的数据库操作Mapper
* @createDate 2024-06-09 15:37:34
* @Entity com.example.entity.OrderReturnApply
*/
public interface OrderReturnApplyMapper extends BaseMapper<OrderReturnApply> {
    @Select("SELECT * FROM order_return_apply")
    List<ReturnApplyDto> getReturnList();

    @Select({"<script>",
            "SELECT *",
            "FROM order_return_apply ",
            "<where>",
            "<if test='orderNo != null and orderNo.trim() != \"\"'>",
            "AND order_sn LIKE CONCAT('%', #{orderNo}, '%')",
            "</if>",
            "<if test='memberName != null and memberName.trim() != \"\"'>",
            "AND return_name LIKE CONCAT('%', #{memberName}, '%')",
            "</if>",
            "<if test='status != null'>",
            "AND status = #{status}",
            "</if>",
            "</where>",
            "</script>"})
    List<ReturnApplyDto> searchReturn(OrderSearchDto orderSearchDto);

    @Select("SELECT * FROM order_return_apply WHERE id = #{id}")
    ReturnApplyDto getReturnDetailById(Long id);

    @Select("SELECT * FROM order_return_apply WHERE store_id = #{storeId}")
    List<ReturnApplyDto>getReturnListByStoreId(Long storeId);

    @Select({"<script>",
            "SELECT *",
            "FROM order_return_apply ",
            "<where>",
            "<if test='storeId != null'>",
            "AND store_id = #{storeId}",
            "</if>",
            "<if test='orderSearchDto.orderNo != null and orderSearchDto.orderNo.trim() != \"\"'>",
            "AND order_sn LIKE CONCAT('%', #{orderSearchDto.orderNo}, '%')",
            "</if>",
            "<if test='orderSearchDto.memberName != null and orderSearchDto.memberName.trim() != \"\"'>",
            "AND return_name LIKE CONCAT('%', #{orderSearchDto.memberName}, '%')",
            "</if>",
            "<if test='orderSearchDto.status != null'>",
            "AND status = #{orderSearchDto.status}",
            "</if>",
            "</where>",
            "</script>"})
    List<ReturnApplyDto> searchReturnByStoreId(Long storeId, OrderSearchDto orderSearchDto);

    @Update("UPDATE order_return_apply SET status = #{status}, handle_note = #{handleNote} WHERE id = #{id} AND status = 0")
    int updateOrderReturn(Long id, String handleNote, Integer status);
}




